package top.zuli.package02.homework14;

public class AccountTest3 {
    public static void main(String[] args) {
        Runnable runnable = new Account3();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
class Account3 implements Runnable{
    private int money = 0;
    private Object obj = new Object();
    @Override
    public void run() {
        for (int i = 1; i <=3; i++) {
            synchronized (this){
                if(money < 3000){
                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    money += 1000;
                    System.out.println(Thread.currentThread().getName()+"：存钱成功。余额为："+money);
                }
            }
        }
    }
}
